package com.yonyou.pmclouds.workstopreport;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.team.rmiitf.ProjectRefWithTeamQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopMaintain;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@Slf4j
@RequestMapping({"/mobile/workStop","/workStop"})
@RestController
public class WorkStopController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private WorkStopMaintain workStopMaintain;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private WorkStopQuery workStopQuery;

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectRefWithTeamQuery query;


    @Autowired
    private UserInfoQuery userQuery;

    @Autowired
    private UserInfoQuery userInfoQuery;

    private static final String[] USER_ID_FIELDS = {WorkStopVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {WorkStopVO.CREATOR_NAME};

    private static final String[] COPYPERSON_ID_FIELDS = {WorkStopVO.COPYPERSON};
    private static final String[] COPYPERSON_NAME_FIELDS = {WorkStopVO.COPYPERSONNAME};

    @Insert
    @PMApprove
    @RequestMapping("/add")
    public WorkStopVO add(@RequestBody WorkStopVO workStopVO) throws BusinessException {

        initInsertVO(workStopVO);

        transCreatorName(workStopVO);

        WorkStopVO result = workStopMaintain.insert(workStopVO);

        transCopyPersonName(result);

        return result;
    }

    //通过报告ID查询停工报告
    @RequestMapping("/getReportById")
    public Object getReportByProjectId(String reportId) throws BusinessException {
        //调用Service的方法
        WorkStopVO workStopVO = workStopQuery.getReport(reportId);

        transCopyPersonName(workStopVO);

        //翻译创建者姓名
        userQuery.tranUserInfoField(new WorkStopVO[]{workStopVO}, USER_ID_FIELDS, USER_NAME_FIELDS);

        return workStopVO;
    }

    @PMApprove(trigger = "commit")
    @RequestMapping("/update")
    public WorkStopVO update(@RequestBody WorkStopVO workStopVO) throws BusinessException {

        WorkStopVO update = workStopMaintain.update(workStopVO);

        userQuery.tranUserInfoField(new WorkStopVO[]{update}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return update;
    }

    @RequestMapping("/remove")
    public void remove(@RequestBody WorkStopVO workStopVO) throws BusinessException {
        workStopMaintain.delete(workStopVO);
    }

    private void initInsertVO(WorkStopVO vo){
        if(vo == null) return;
        vo.setBillType(BillTypeConst.WORKSTOP_BILLTYPE);
        vo.setBillstatus(BillStatusConst.COMMON_FREE_STATUS);
    }

    private void transCreatorName(@RequestBody WorkStopVO workStopVO) {
        String creatorName = userInfoQuery.queryUserName(RuntimeEnvironment.getLoginUserId());
        workStopVO.setCreatorName(creatorName);
    }

    private void transCopyPersonName(WorkStopVO result) {
        List<CopyPersonVO> copyPerson = result.getCopyPersons();
        if (null != copyPerson && copyPerson.size() > 0) {
            userQuery.tranUserInfoField(copyPerson.toArray(new CopyPersonVO[0]), COPYPERSON_ID_FIELDS, COPYPERSON_NAME_FIELDS);
        }
    }
}
